package com.jinyu.jyly.net;

import android.util.Log;

import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkhttpInfo {

    private void setIkhttp(){
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        OkHttpClient mOkHttpClient = clientBuilder.build();

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cid", 37);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String ss = AES.encryptToHexString(jsonObject.toString());
//            MediaType mediaType = MediaType.parse("application/json; charset=utf-8");//"类型,字节码"
//            RequestBody requestBody =RequestBody.create(mediaType,ss);

            FormBody formBody = new FormBody.Builder()
                    .add("data",ss)
                    .build();

            Request request = new Request.Builder()
                    .post(formBody)
                    .url("http://hec.edschain.vip/site/ad?token=80840ff03980bc534bc4d00cfd640075")
                    .build();
            mOkHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String string = response.body().string();
                    Log.e("aa",string);


                    try {
                        byte key[] = "lisnk2121booboak".getBytes("utf-8");
                        JSONObject jsonObject1 = new JSONObject(string);
                        String data = (String) jsonObject1.opt("data");
                        String decrypt1 = AES.decrypt(data);

                        Log.e("aa",decrypt1);



                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });

    }

    private void setEncrypt() {
//        byte[] decode = Base64.decode(content, Base64.DEFAULT);

////        byte[] ss = AESEncryptUtil.hexParseByte2(content);
//        byte[] decrypt = AESEncryptUtil.decrypt(decode, "lisnk2121booboak1qaz*WSX");
//        Log.e("解密内容：", new String(decrypt));


        try {
//            byte[] decrypt = Aes.encrypt(content, key);
//            String ss = AESEncryptUtil.byte2ParseHex(decrypt);
//            byte[] bytes = AESEncryptUtil.hexParseByte2(content01);
            String decrypt = AES.decrypt("content01");
            Log.e("解密：",decrypt);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
